package org.example.web.admin.controller.apartment;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.common.result.Result;
import org.example.common.result.ResultCodeEnum;
import org.example.model.entity.PaymentType;
import org.example.web.admin.service.PaymentTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "支付方式管理")
@RequestMapping("/admin/payment")
@RestController
public class PaymentTypeController {

    @Autowired
    private PaymentTypeService paymentTypeService;

    @Operation(summary = "查询全部支付方式列表")
    @GetMapping("list")
    public Result<List<PaymentType>> listPaymentType() {
        List<PaymentType> list = paymentTypeService.list();
        return Result.ok(list);
    }

    @Operation(summary = "保存或更新支付方式")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdatePaymentType(@RequestBody PaymentType paymentType) {
    //public Result saveOrUpdatePaymentType(PaymentType paymentType) {
        //@RequestBody是以JSON传递的(不能是GET请求,因为其没有请求体)
        //不用注释时是用拼接的形式进行传递的
        boolean flag = paymentTypeService.saveOrUpdate(paymentType);
        return flag ? Result.ok() : Result.fail();
    }

    @Operation(summary = "根据ID删除支付")
    @DeleteMapping("deleteById")
    public Result deletePaymentById(@RequestParam Long id) {
        boolean flag = paymentTypeService.removeById(id);
        return flag ? Result.ok() : Result.fail();
    }

    @Operation(summary = "根据ID查询支付方式")
    @GetMapping("getPaymentType/{id}")
    public Result getPaymentType(@PathVariable Long id) {
        PaymentType paymentType = paymentTypeService.getById(id);
        return Result.ok(paymentType);
    }

    @Operation(summary = "仅更新支付方式")
    @PostMapping("update")
    public Result updatePaymentType(@RequestBody PaymentType paymentType) {
        // 检查ID是否存在
        if (paymentType.getId() == null) {
            return Result.build("更新时ID不能为空", ResultCodeEnum.PARAM_ERROR);
        }

        // 检查记录是否存在
        PaymentType existing = paymentTypeService.getById(paymentType.getId());
        if (existing == null) {
            return Result.build("要更新的支付方式不存在", ResultCodeEnum.DATA_ERROR);
        }

        boolean flag = paymentTypeService.updateById(paymentType);

        //更新完成后将试图对象返回
        PaymentType updated = paymentTypeService.getById(paymentType.getId());


        return flag ? Result.ok(updated) : Result.fail();
    }
}















